package com.sky.sky_server.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sky.sky_common.context.BaseContext;
import com.sky.sky_pojo.dto.ShoppingCartDTO;
import com.sky.sky_pojo.entity.Dish;
import com.sky.sky_pojo.entity.Setmeal;
import com.sky.sky_pojo.entity.ShoppingCart;
import com.sky.sky_server.mapper.DishMapper;
import com.sky.sky_server.mapper.SetmealMapper;
import com.sky.sky_server.mapper.ShoppingCartMapper;
import com.sky.sky_server.service.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     */
    @Override
    @Transactional
    public int addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 购物车存在，则增加数量
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByDishIdOrSetmealId(shoppingCart);
        // 查询到的数据只能是 0 或者 1
        if(shoppingCartList != null && shoppingCartList.size() > 0) {
            // 增加对应数量
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            return shoppingCartMapper.update(shoppingCart);
        }

        // 购物车中不存在，则添加
        shoppingCart.setNumber(1);
        // 创建时间
        shoppingCart.setCreateTime(LocalDateTime.now());

        // 增加冗余字段，需要先判断添加的是分类还是菜品
        // 当前添加的是套餐
        if(shoppingCart.getSetmealId() != null) {
            // 查询套餐信息
            Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
            if(setmeal == null){
                return -1;
            }
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        // 当前添加的是菜品
        else if(shoppingCart.getDishId() != null) {
            Dish dish = dishMapper.selectById(shoppingCart.getDishId());
            if(dish == null){
                return -1;
            }
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }
        // 添加到购物车
        return shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    /**
     * 删除购物车内的一个商品
     */
    @Override
    public int delete(ShoppingCartDTO shoppingCartDTO) {
        if(shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() == null){
            log.error("删除购物车内的一个商品：菜品id和套餐id不能同时为空");
            return -1;
        } 
        // 判断该条记录是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByDishIdOrSetmealId(shoppingCart);
        // 若该条记录存在，判断其数量是否大于1
        if(shoppingCarts != null && shoppingCarts.size() > 0) {
            shoppingCart = shoppingCarts.get(0);
            int res = 0;
            if(shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                res = shoppingCartMapper.update(shoppingCart);
            }else{
                res = shoppingCartMapper.delete(shoppingCart);
            }
            return res;
        }else{
            return -1;
        }



    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }
}
